package controllers;

import java.text.ParseException;

import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import views.html.pageErreurConnexion;
import views.html.ConsoleAdmin.*;

import models.*;

/**
 * Manage a database of qcms
 */
public abstract class AdminQcm extends Controller {

	/**
	 * This result directly redirect to application home.
	 */
	public final static Result GO_HOME = redirect(routes.AdminQcm.listQcm(0,
			"id_qcm", "asc", ""));

	/**
	 * Handle default path requests, redirect to qcms list
	 */
	public static Result indexQcm() {
		if (Application.estAdmin()) {
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the paginated list of qcms.
	 * 
	 * @param page
	 *            Current page number (starts from 0)
	 * @param sortBy
	 *            Column to be sorted
	 * @param order
	 *            Sort order (either asc or desc)
	 * @param filter
	 *            Filter applied on qcm names
	 */
	public static Result listQcm(final int page, final String sortBy,
			final String order, final String filter) {
		if (Application.estAdmin()) {
			return ok(listQcm.render(Qcm.page(page, 10, sortBy, order, filter),
					sortBy, order, filter));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'edit form' of a existing Qcm.
	 * 
	 * @param id
	 *            Id of the qcm to edit
	 */
	public static Result editQcm(final Long id) {
		if (Application.estAdmin()) {
			final Form<Qcm> qcmForm = form(Qcm.class).fill(Qcm.findById(id));
			return ok(editQcm.render(id, qcmForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'edit form' submission
	 * 
	 * @param id
	 *            Id of the qcm to edit
	 * @throws ParseException
	 */
	public static Result updateQcm(final Long id) throws ParseException {
		if (Application.estAdmin()) {
			final Form<Qcm> qcmForm = form(Qcm.class).bindFromRequest();
			if (qcmForm.hasErrors()) {
				return badRequest(editQcm.render(id, qcmForm));
			}

			DynamicForm filledForm = form().bindFromRequest();
			if (filledForm.get("dateCloture") != null)
				qcmForm.get().setDateCloture(
						Utile.stringToDate(filledForm.get("dateCloture")));
			if (filledForm.get("dateCreation") != null)
				qcmForm.get().setDateCreation(
						Utile.stringToDate(filledForm.get("dateCreation")));
			if (filledForm.get("dateOuverture") != null)
				qcmForm.get().setDateOuverture(
						Utile.stringToDate(filledForm.get("dateOuverture")));

			qcmForm.get().update(id);
			flash("success", "Qcm " + qcmForm.get().getLibelle()
					+ " has been updated");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'new qcm form'.
	 */
	public static Result createQcm() {
		if (Application.estAdmin()) {
			final Form<Qcm> qcmForm = form(Qcm.class);
			return ok(createQcm.render(qcmForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'new qcm form' submission
	 */
	public static Result saveQcm() throws ParseException {
		if (Application.estAdmin()) {
			final Form<Qcm> qcmForm = form(Qcm.class).bindFromRequest();
			if (qcmForm.hasErrors()) {
				return badRequest(createQcm.render(qcmForm));
			}
			DynamicForm filledForm = form().bindFromRequest();
			if (filledForm.get("dateCloture") != null)
				qcmForm.get().setDateCloture(
						Utile.stringToDate(filledForm.get("dateCloture")));
			if (filledForm.get("dateCreation") != null)
				qcmForm.get().setDateCreation(
						Utile.stringToDate(filledForm.get("dateCreation")));
			if (filledForm.get("dateOuverture") != null)
				qcmForm.get().setDateOuverture(
						Utile.stringToDate(filledForm.get("dateOuverture")));

			qcmForm.get().save();
			flash("success", "Qcm " + qcmForm.get().getLibelle()
					+ " has been created");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle qcm deletion
	 */
	public static Result deleteQcm(final Long id) {
		if (Application.estAdmin()) {
			Qcm.delete(id);
			flash("success", "Qcm has been deleted");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

}
